package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AlertDialog;

import com.bnyy.medicalHousekeeper.DialogHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;

import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/11/20
 **/
public class EditGroupChatNameActivity extends BaseActivityImpl {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_count)
    TextView tvCount;

    public static void show(Context context, int groupChatId, String groupChatName, ActivityResultLauncher<Intent> launcher) {
        Intent intent = new Intent(context, EditGroupChatNameActivity.class);
        intent.putExtra("groupChatName", groupChatName);
        intent.putExtra("groupChatId", groupChatId);
        launcher.launch(intent);
    }

    public static void show(Context context, int groupChatId, String groupChatName) {
        Intent intent = new Intent(context, EditGroupChatNameActivity.class);
        intent.putExtra("groupChatName", groupChatName);
        intent.putExtra("groupChatId", groupChatId);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_group_chat_name;
    }

    @Override
    public String getTitleStr() {
        return "修改群聊名称";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int groupChatId = getIntent().getIntExtra("groupChatId", -1);
        String groupChatName = getIntent().getStringExtra("groupChatName");

        TextView tvConfirm = new TextView(mContext);
        tvConfirm.setBackgroundResource(R.drawable.selector_button_corner_green_180);
        tvConfirm.setText("确定");
        tvConfirm.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        tvConfirm.setTextColor(Color.WHITE);
        tvConfirm.setPadding(15, 5, 15, 5);
        tvConfirm.setEnabled(false);
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = etName.getText().toString();
                DialogHelper.showNormalDialog(mContext, "确认修改群聊名称为【" + s + "】吗？",
                        new DialogHelper.Callback() {
                            @Override
                            public void onRightClick(AlertDialog alertDialog) {
                                super.onRightClick(alertDialog);

                                if (s.equals(groupChatName)) {
                                    finish();
                                } else {
                                    HashMap<String, Object> params = new HashMap<>();
                                    params.put("id", groupChatId);
                                    params.put("group_name", s);
                                    requestManager.request(requestManager.mMessageRetrofitService.editGroupChatSetting(RequestManager.getJsonRequestBody(params)),
                                            new BaseObserverImpl<Object>(mContext) {
                                                @Override
                                                public void onSuccess() {
                                                    super.onSuccess();
                                                    Toast.makeText(mContext, "修改成功", Toast.LENGTH_SHORT).show();
                                                    Intent intent = new Intent();
                                                    intent.putExtra("groupChatName", s);
                                                    setResult(RESULT_OK, intent);
                                                    finish();
                                                }
                                            });
                                }

                            }
                        });
            }
        });
        setOptions(tvConfirm);

        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    tvCount.setText("0/12");
                } else {
                    tvCount.setText(s.length() + "/12");
                }
                tvConfirm.setEnabled(!TextUtils.isEmpty(s));
            }
        });

        etName.setText(groupChatName);
    }
}


































